using System;
using UnityEngine;
using UnityEngine.UI;

namespace MaterialUI
{
	[AddComponentMenu("MaterialUI/Dialogs/Time Picker", 1)]
	public class DialogTimePicker : MaterialDialog
	{
		[SerializeField]
		private Image m_ClockNeedle;

		[SerializeField]
		private Text[] m_ClockTextArray;

		[SerializeField]
		private Text m_TextAM;

		[SerializeField]
		private Text m_TextPM;

		[SerializeField]
		private Text m_TextHours;

		[SerializeField]
		private Text m_TextMinutes;

		private int m_CurrentHour;

		private int m_CurrentMinute;

		private bool m_IsAM;

		private bool m_IsHoursSelected;

		private Action<DateTime> m_OnAffirmativeClicked;

		[SerializeField]
		private MaterialButton m_AffirmativeButton;

		[SerializeField]
		private MaterialButton m_DismissiveButton;

		[SerializeField]
		private Image m_Header;

		[SerializeField]
		private VectorImage m_NeedleCenter;

		[SerializeField]
		private VectorImage m_NeedleEnd;

		[SerializeField]
		private DialogTimePickerClock m_timePickerClock;

		private float m_NeedleAngle;

		private Text m_ClosestText;

		[SerializeField]
		private Text m_MaskedText;

		public Image clockNeedle => m_ClockNeedle;

		public Text[] clockTextArray => m_ClockTextArray;

		public Text textAM => m_TextAM;

		public Text textPM => m_TextPM;

		public Text textHours => m_TextHours;

		public Text textMinutes => m_TextMinutes;

		public int currentHour
		{
			get
			{
				return m_CurrentHour;
			}
			set
			{
				SetHours(value);
			}
		}

		public int currentMinute
		{
			get
			{
				return m_CurrentMinute;
			}
			set
			{
				SetMinutes(value);
			}
		}

		public bool isAM
		{
			get
			{
				return m_IsAM;
			}
			set
			{
				if (value)
				{
					OnAMClicked();
				}
				else
				{
					OnPMClicked();
				}
			}
		}

		public bool isHoursSelected
		{
			get
			{
				return m_IsHoursSelected;
			}
			set
			{
				if (value)
				{
					OnHoursClicked();
				}
				else
				{
					OnMinutesClicked();
				}
			}
		}

		public Action<DateTime> onAffirmativeClicked
		{
			get
			{
				return m_OnAffirmativeClicked;
			}
			set
			{
				m_OnAffirmativeClicked = value;
			}
		}

		public MaterialButton affirmativeButton
		{
			get
			{
				return m_AffirmativeButton;
			}
			set
			{
				m_AffirmativeButton = value;
			}
		}

		public MaterialButton dismissiveButton
		{
			get
			{
				return m_DismissiveButton;
			}
			set
			{
				m_DismissiveButton = value;
			}
		}

		public Image header
		{
			get
			{
				return m_Header;
			}
			set
			{
				m_Header = value;
			}
		}

		public VectorImage needleCenter
		{
			get
			{
				return m_NeedleCenter;
			}
			set
			{
				m_NeedleCenter = value;
			}
		}

		public VectorImage needleEnd
		{
			get
			{
				return m_NeedleEnd;
			}
			set
			{
				m_NeedleEnd = value;
			}
		}

		public void Initialize(DateTime time, Action<DateTime> onAffirmativeClicked, Color accentColor)
		{
			currentHour = time.Hour;
			currentMinute = time.Minute;
			isAM = isAM;
			this.onAffirmativeClicked = onAffirmativeClicked;
			Vector2 sizeDelta = base.rectTransform.sizeDelta;
			Initialize();
			base.rectTransform.sizeDelta = sizeDelta;
			SetColor(accentColor);
		}

		public override void Show()
		{
			base.callbackShowAnimationOver = (Action)Delegate.Combine(base.callbackShowAnimationOver, (Action)delegate
			{
				m_timePickerClock.Init();
			});
			base.Show();
		}

		public void SetColor(Color accentColor)
		{
			m_ClockNeedle.color = accentColor;
			m_NeedleCenter.color = accentColor;
			m_NeedleEnd.color = accentColor;
			m_Header.color = accentColor;
			m_AffirmativeButton.materialRipple.rippleData.Color = accentColor;
			m_DismissiveButton.materialRipple.rippleData.Color = accentColor;
		}

		private void Update()
		{
			m_ClockNeedle.transform.localRotation = Quaternion.Slerp(m_ClockNeedle.transform.localRotation, Quaternion.Euler(new Vector3(0f, 0f, m_NeedleAngle)), Time.deltaTime * 20f);
			Vector3 eulerAngles = m_ClockNeedle.transform.eulerAngles;
			int num = GetHourFromAngle(eulerAngles.z) - 1;
			if (num == -1)
			{
				num = 11;
			}
			m_ClosestText = m_ClockTextArray[num];
			m_MaskedText.transform.position = m_ClosestText.transform.position;
			m_MaskedText.transform.rotation = m_ClosestText.transform.rotation;
			m_MaskedText.text = m_ClosestText.text;
		}

		private float GetAngleFromHour(int hour)
		{
			return -m_CurrentHour * 30 + 90;
		}

		private int GetHourFromAngle(float angle)
		{
			float f = (0f - (angle - 90f)) / 30f;
			int num = Mathf.RoundToInt(f);
			if (num < 0)
			{
				num += 12;
			}
			return num;
		}

		private float GetAngleFromMinute(int minute)
		{
			return (float)(-m_CurrentMinute) / 5f * 30f + 90f;
		}

		private int GetMinuteFromAngle(float angle)
		{
			float f = (0f - (angle - 90f)) / 30f * 5f;
			int num = Mathf.RoundToInt(f);
			if (num < 0)
			{
				num += 60;
			}
			return num;
		}

		public void SetAngle(float angle)
		{
			if (m_IsHoursSelected)
			{
				SetHours(GetHourFromAngle(angle));
			}
			else
			{
				SetMinutes(GetMinuteFromAngle(angle));
			}
		}

		private void SetHours(int hour, bool updateClock = true)
		{
			if (hour <= 0)
			{
				hour = 12;
			}
			if (hour > 12)
			{
				hour = 12;
			}
			m_CurrentHour = hour;
			textHours.text = hour.ToString("0");
			if (updateClock)
			{
				SelectHours();
				UpdateNeedle();
			}
		}

		private void SetMinutes(int minute, bool updateClock = true)
		{
			if (minute < 0)
			{
				minute = 0;
			}
			if (minute > 60)
			{
				minute = 60;
			}
			m_CurrentMinute = minute;
			m_TextMinutes.text = minute.ToString("00");
			if (updateClock)
			{
				SelectMinutes();
				UpdateNeedle();
			}
		}

		public void SetTime(int hour, int minute)
		{
			SetHours(hour);
			SetMinutes(minute);
		}

		private void UpdateNeedle()
		{
			float angle = (!m_IsHoursSelected) ? GetAngleFromMinute(m_CurrentMinute) : GetAngleFromHour(m_CurrentHour);
			UpdateNeedleAngle(angle);
		}

		private void UpdateNeedleAngle(float angle)
		{
			m_NeedleAngle = angle;
		}

		private void UpdateClockTextArray()
		{
			for (int i = 0; i < m_ClockTextArray.Length; i++)
			{
				if (m_IsHoursSelected)
				{
					m_ClockTextArray[i].text = (i + 1).ToString("0");
					continue;
				}
				int num = (i + 1) * 5;
				num %= 60;
				m_ClockTextArray[i].text = num.ToString("00");
			}
			UpdateNeedle();
		}

		public void OnAMClicked()
		{
			m_IsAM = true;
			Text textAM = m_TextAM;
			Color color = m_TextAM.color;
			float r = color.r;
			Color color2 = m_TextAM.color;
			float g = color2.g;
			Color color3 = m_TextAM.color;
			textAM.color = new Color(r, g, color3.b, 1f);
			Text textPM = this.textPM;
			Color color4 = this.textPM.color;
			float r2 = color4.r;
			Color color5 = this.textPM.color;
			float g2 = color5.g;
			Color color6 = this.textPM.color;
			textPM.color = new Color(r2, g2, color6.b, 0.5f);
		}

		public void OnPMClicked()
		{
			m_IsAM = false;
			Text textPM = this.textPM;
			Color color = this.textPM.color;
			float r = color.r;
			Color color2 = this.textPM.color;
			float g = color2.g;
			Color color3 = this.textPM.color;
			textPM.color = new Color(r, g, color3.b, 1f);
			Text textAM = m_TextAM;
			Color color4 = m_TextAM.color;
			float r2 = color4.r;
			Color color5 = m_TextAM.color;
			float g2 = color5.g;
			Color color6 = m_TextAM.color;
			textAM.color = new Color(r2, g2, color6.b, 0.5f);
		}

		public void OnHoursClicked()
		{
			SelectHours();
		}

		private void SelectHours()
		{
			Text textHours = this.textHours;
			Color color = this.textHours.color;
			float r = color.r;
			Color color2 = this.textHours.color;
			float g = color2.g;
			Color color3 = this.textHours.color;
			textHours.color = new Color(r, g, color3.b, 1f);
			Text textMinutes = m_TextMinutes;
			Color color4 = m_TextMinutes.color;
			float r2 = color4.r;
			Color color5 = m_TextMinutes.color;
			float g2 = color5.g;
			Color color6 = m_TextMinutes.color;
			textMinutes.color = new Color(r2, g2, color6.b, 0.5f);
			m_IsHoursSelected = true;
			UpdateClockTextArray();
		}

		public void OnMinutesClicked()
		{
			SelectMinutes();
		}

		private void SelectMinutes()
		{
			Text textMinutes = m_TextMinutes;
			Color color = m_TextMinutes.color;
			float r = color.r;
			Color color2 = m_TextMinutes.color;
			float g = color2.g;
			Color color3 = m_TextMinutes.color;
			textMinutes.color = new Color(r, g, color3.b, 1f);
			Text textHours = this.textHours;
			Color color4 = this.textHours.color;
			float r2 = color4.r;
			Color color5 = this.textHours.color;
			float g2 = color5.g;
			Color color6 = this.textHours.color;
			textHours.color = new Color(r2, g2, color6.b, 0.5f);
			m_IsHoursSelected = false;
			UpdateClockTextArray();
		}

		public void OnButtonOkClicked()
		{
			if (m_OnAffirmativeClicked != null)
			{
				m_OnAffirmativeClicked(DateTime.MinValue.AddHours(m_CurrentHour).AddMinutes(m_CurrentMinute));
			}
			Hide();
		}
	}
}
